  import NotesView from "./view";
  import NotesAPI from "./api";

  export default class App {
    constructor(root) {
      this.notes = [];
      this.activeNote = null;
      this.view = new NotesView(root, this._handlers());

      this._refreshNotes();
    }

    _refreshNotes() {
      const notes = NotesAPI.getAllNotes();

      this._setNotes(notes);

      if (notes.length > 0) {
        this._setActiveNote(notes[0]);
      }
    }

    _setNotes(notes) {
      this.notes = notes;
      this.view.updateNoteList(notes);
      this.view.updateNotePreviewVisibility(notes.length > 0);
    }

    _setActiveNote(note) {
      console.log("notes", note);
      this.activeNote = note;
      this.view.updateActiveNote(note);
    }

    _handlers() {
      return {
        onNoteSelect: (noteId) => {
          console.log(noteId, this.notes[0].id);
          const selectedNote = this.notes.find(
            (note) => note.id === noteId
          );
          console.log("selectedNote", selectedNote);
          this._setActiveNote(selectedNote);
        },
        onNoteAdd: () => {
          const newNote = {
            title: "新建笔记",
            body: "开始记录...",
          };

          NotesAPI.saveNote(newNote);
          this._refreshNotes();
        },
        onNoteEdit: (title, body) => {
          NotesAPI.saveNote({
            id: this.activeNote.id,
            title,
            body,
          });

          this._refreshNotes();
        },
        onNoteDelete: (noteId) => {
          NotesAPI.deleteNote(noteId);
          this._refreshNotes();
        },
      };
    }
  }
